package com.example.bootdemo5.config;

import com.example.bootdemo5.intercepter.TokeValidIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
@EnableWebMvc
public class WebMvcConfig  implements WebMvcConfigurer {


    @Autowired
    TokeValidIntercepter tokeValidIntercepter;



   public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                allowedOrigins("*").allowCredentials(false).
               allowedHeaders("*").allowedMethods("*").maxAge(3600);

   }

    /**
     * 添加token验证器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokeValidIntercepter).addPathPatterns("/api/**")
                .excludePathPatterns("/api/login","/api/register");

    }

    /**
     * 给多有的请求路径添加前缀
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 设置所有的控制器方法默认前缀 /api
        configurer.addPathPrefix("/api", HandlerTypePredicate.forAnnotation(RestController.class));
    }
}
